package com.woyou.model;

import java.nio.charset.CoderResult;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.EBean.Scope;

import com.woyou.bean.CodeListResult;
import com.woyou.bean.CodeResult;
import com.woyou.bean.Comment;
import com.woyou.bean.Coupon;
import com.woyou.bean.DeliverTime;
import com.woyou.bean.Goods;
import com.woyou.bean.OrderDetail;
import com.woyou.bean.OrderGoodsItem;
import com.woyou.bean.OrderStateLog;
import com.woyou.bean.Result;
import com.woyou.bean.rpc.CouponReq;
import com.woyou.bean.rpc.MD5Req;
import com.woyou.bean.rpc.OrderCommentsReq;
import com.woyou.bean.rpc.OrderDefaultReq;
import com.woyou.bean.rpc.PayResultReq;
import com.woyou.bean.rpc.QueryActUrlReq;
import com.woyou.bean.rpc.QueryActUrlRes;
import com.woyou.bean.rpc.QueryOrderNoReq;
import com.woyou.bean.rpc.QueryOrderNoRes;
import com.woyou.bean.rpc.SendOrderReq;
import com.woyou.bean.rpc.SendOrderRes;
import com.woyou.bean.rpc.ShoppingTimeReq;
import com.woyou.bean.rpc.orderConfirmReq;
import com.woyou.model.rpc.OrderService;

import retrofit.RetrofitError;

@SuppressWarnings({ "rawtypes", "unused" })
@EBean(scope = Scope.Singleton)
public class OrderModel extends SuperModel {
	private OrderService orderService;

	// 在构造函数调用之后执行
	@AfterInject
	@Override
	public void onInit() {
		orderService = mNeWrapper.getNetService(OrderService.class);
	}

	/**
	 * 发送订单
	 * 
	 * @param order
	 * @return
	 * @throws RetrofitError
	 */
	public Result<SendOrderRes> v2_3sendOrder(SendOrderReq orderReq) throws RetrofitError {
		MD5Req<SendOrderReq> md5Req = new MD5Req<SendOrderReq>(orderReq);
		Result<SendOrderRes> result = orderService.v2_3sendOrder(md5Req.jsonParams, md5Req.isEncrypted,
				md5Req.timeStamp, md5Req.randomNum, md5Req.sign);
		// Result<SendOrderRes> result = mockv2_3snedOrder();
		return result;
	}

	// mock v2_3sendOrder
	public Result<SendOrderRes> mockv2_3snedOrder() {
		Result<SendOrderRes> result = new Result<SendOrderRes>();
		result.code = 1;
		result.msg = "成功";
		SendOrderRes res = new SendOrderRes();
		res.setCreateTime(1441769253);
		res.setOrderNo("order001");
		res.setGoodsList(new ArrayList<Goods>());
		result.data = res;
		return result;
	}

	/**
	 * 查询订单状态
	 * 
	 * @param orderNo
	 * @return
	 * @throws RetrofitError
	 */
	public CodeResult<QueryOrderNoRes> queryOrderStatus(QueryOrderNoReq orderNoReq) throws RetrofitError {
		MD5Req<QueryOrderNoReq> md5Req = new MD5Req<QueryOrderNoReq>(orderNoReq);
		CodeResult<QueryOrderNoRes> result = orderService.queryOrderStatus(md5Req.jsonParams, md5Req.isEncrypted,
				md5Req.timeStamp, md5Req.randomNum, md5Req.sign);
		return result;
	}

	/**
	 * 获取我的优惠券
	 * 
	 * @param uid
	 * @param lat
	 * @param lng
	 * @return
	 * @throws RetrofitError
	 */
	public CodeListResult<Coupon> queryCoupons(CouponReq couponReq) throws RetrofitError {
		MD5Req<CouponReq> md5Req = new MD5Req<CouponReq>(couponReq);
		CodeListResult<Coupon> result = orderService.queryCoupons(md5Req.jsonParams, md5Req.isEncrypted,
				md5Req.timeStamp, md5Req.randomNum, md5Req.sign);
		return result;
	}

	/**
	 * 获取营业时间段
	 * 
	 * @param couponReq
	 * @return
	 * @throws RetrofitError
	 */
	public CodeListResult<DeliverTime> getShoppingTimeList(ShoppingTimeReq shoppingTimeReq) throws RetrofitError {
		MD5Req<ShoppingTimeReq> md5Req = new MD5Req<ShoppingTimeReq>(shoppingTimeReq);
		CodeListResult<DeliverTime> result = orderService.queryTime(md5Req.jsonParams, md5Req.isEncrypted,
				md5Req.timeStamp, md5Req.randomNum, md5Req.sign);
		return result;
	}

	/**
	 * 2.3 获取订单详情（http://xxxx/ v2_1queryOrderDetail）
	 * 
	 * @param orderDetailReq
	 * @return
	 * @throws RetrofitError
	 */
	public Result<OrderDetail> v2_3queryOrderDetail(OrderDefaultReq orderDetailReq) throws RetrofitError {
		MD5Req<OrderDefaultReq> md5Req = new MD5Req<OrderDefaultReq>(orderDetailReq);
		Result<OrderDetail> result = orderService.v2_3queryOrderDetail(md5Req.jsonParams, md5Req.isEncrypted,
				md5Req.timeStamp, md5Req.randomNum, md5Req.sign);
		// Result<OrderDetail> result = mockQueryOrderDetail();
		return result;
	}

	/**
	 * 2.23 催单（http://xxxx/reminder）
	 * 
	 * @param orderDefaultReq
	 * @return
	 * @throws RetrofitError
	 */
	public CodeResult reminderOrder(OrderDefaultReq orderDefaultReq) throws RetrofitError {
		MD5Req<OrderDefaultReq> md5Req = new MD5Req<OrderDefaultReq>(orderDefaultReq);
		CodeResult result = orderService.reminder(md5Req.jsonParams, md5Req.isEncrypted, md5Req.timeStamp,
				md5Req.randomNum, md5Req.sign);
		return result;
	}

	/**
	 * 2.22 确认收货（http://xxxx/orderConfirm）
	 * 
	 * @param orderReportReq
	 * @return
	 * @throws RetrofitError
	 */
	public CodeResult orderConfirm(orderConfirmReq orderConfirmReq) throws RetrofitError {
		MD5Req<orderConfirmReq> md5Req = new MD5Req<orderConfirmReq>(orderConfirmReq);
		CodeResult result = orderService.orderConfirm(md5Req.jsonParams, md5Req.isEncrypted, md5Req.timeStamp,
				md5Req.randomNum, md5Req.sign);
		return result;
	}

	/**
	 * 2.18 订单评论（http://xxxx/orderComments）
	 * 
	 * @param orderCommentsReq
	 * @return
	 * @throws RetrofitError
	 */
	public CodeResult orderComments(OrderCommentsReq orderCommentsReq) throws RetrofitError {
		MD5Req<OrderCommentsReq> md5Req = new MD5Req<OrderCommentsReq>(orderCommentsReq);
		CodeResult result = orderService.orderComments(md5Req.jsonParams, md5Req.isEncrypted, md5Req.timeStamp,
				md5Req.randomNum, md5Req.sign);
		return result;
	}

	/**
	 * 2.5 取消订单（http://xxxx/ v2_1cancelOrder）
	 * 
	 * @param OrderDefaultReq
	 * @return
	 * @throws RetrofitError
	 */
	public Result v2_1cancelOrder(OrderDefaultReq orderDefaultReq) throws RetrofitError {
		MD5Req<OrderDefaultReq> md5Req = new MD5Req<OrderDefaultReq>(orderDefaultReq);
		Result result = orderService.v2_1cancelOrder(md5Req.jsonParams, md5Req.isEncrypted, md5Req.timeStamp,
				md5Req.randomNum, md5Req.sign);
		return result;
	}

	/**
	 * 2.27 根据订单查询市场活动（http://xxxx/queryActByOrder）
	 * 
	 * @return
	 */
	public CodeResult<QueryActUrlRes> queryActByOrder(QueryActUrlReq queryActUrlReq) throws RetrofitError {
		MD5Req<QueryActUrlReq> md5Req = new MD5Req<QueryActUrlReq>(queryActUrlReq);
		CodeResult<QueryActUrlRes> result = orderService.queryActByOrder(md5Req.jsonParams, md5Req.isEncrypted,
				md5Req.timeStamp, md5Req.randomNum, md5Req.sign);
		return result;
	}

	/**
	 * 2.6重试推送订单（http://xxxx/v2_3RetryPushOrder） TODO (在线支付，重新推送订单)
	 * 
	 * @param orderDefaultReq
	 * @return
	 * @throws RetrofitError
	 *             CodeResult
	 */
	public CodeResult onlinePayRetryPushOrder(OrderDefaultReq orderDefaultReq) throws RetrofitError {
		MD5Req<OrderDefaultReq> md5Req = new MD5Req<OrderDefaultReq>(orderDefaultReq);
		CodeResult result = orderService.onlinePayRetryPushOrder(md5Req.jsonParams, md5Req.isEncrypted,
				md5Req.timeStamp, md5Req.randomNum, md5Req.sign);
		// CodeResult result = mockOninePayResult();
		return result;
	}

	/**
	 * TODO 根据订单号查询后台是否已确认在线支付成功
	 * 
	 * @param payResultReq
	 * @return CodeResult
	 */

	public CodeResult v2_3queryPayResult(PayResultReq payResultReq) {
		MD5Req<PayResultReq> md5Req = new MD5Req<PayResultReq>(payResultReq);
		CodeResult result = orderService.queryPayResult(md5Req.jsonParams, md5Req.isEncrypted, md5Req.timeStamp,
				md5Req.randomNum, md5Req.sign);
		// CodeResult result = mockPayResult();
		return result;
	}

	/**
	 * TODO 退款接口
	 * 
	 * @param payResultReq
	 * @return CodeResult
	 */
	public CodeResult refund(PayResultReq payResultReq) {
		MD5Req<PayResultReq> md5Req = new MD5Req<PayResultReq>(payResultReq);
		CodeResult result = orderService.refund(md5Req.jsonParams, md5Req.isEncrypted, md5Req.timeStamp,
				md5Req.randomNum, md5Req.sign);
		// CodeResult result = mockPayResult();
		return result;
	}

	// mock 查询是否支付成功的数据
	private CodeResult mockPayResult() {
		CodeResult result = new CodeResult();
		// Math.random()>0.9?0:1
		result.setCode(1);// 随机mock16种状态中的一种
		try {
			Thread.sleep(1000 * 10);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	// moc在线查询订单返回结果
	private CodeResult mockOninePayResult() {
		CodeResult result = new CodeResult();
		result.setCode(new Random().nextInt(16) + 1);// 随机mock16种状态中的一种
		return result;

	}

	// mock订单详情
	private Result<OrderDetail> mockQueryOrderDetail() {
		Result<OrderDetail> result = new Result<OrderDetail>();
		result.code = 1;
		result.msg = "成功";
		OrderDetail detail = new OrderDetail();
		detail.setoId("SHDP013DC1505190013");
		detail.setsId("SHDP013");
		detail.setsName("6号测试店(上海)152");
		detail.setPayType(1);
		detail.setDeliverFee("0.00");
		detail.setMealsBoxSum("1.0");
		detail.setpCoupon("0");
		detail.setsCoupon("2.2");
		detail.setState(13);
		detail.setCancelType(2);

		OrderStateLog o1 = new OrderStateLog();
		o1.setLogDetails("等待商家接单");
		o1.setLogName("订单已提交，等待支付");
		o1.setLogTime(1020352);

		OrderStateLog o2 = new OrderStateLog();
		o2.setLogDetails("商家23秒接单");
		o2.setLogName("商家接受订单");
		o2.setLogTime(1020352);

		OrderStateLog o3 = new OrderStateLog();
		o3.setLogDetails("商家接受");
		o3.setLogName("申请取消中");
		o3.setLogTime(1020352);

		OrderStateLog o4 = new OrderStateLog();
		o4.setLogDetails("商家不方便取消订单，请谅解");
		o4.setLogName("未取消");
		o4.setLogTime(1020352);

		ArrayList<OrderStateLog> arrayListo = new ArrayList<OrderStateLog>();
		arrayListo.add(o1);
		arrayListo.add(o2);
		arrayListo.add(o3);
		arrayListo.add(o4);
		detail.setOrderStateLog(arrayListo);

		OrderGoodsItem item1 = new OrderGoodsItem();
		item1.setgName("BEE扑克");
		item1.setgId("GL000040");
		item1.setQty(1);
		item1.setPrice(15.88f);
		item1.setSubGList(null);
		item1.setOptList(null);

		OrderGoodsItem item2 = new OrderGoodsItem();
		item2.setgName("一根烟");
		item2.setgId("GL000043");
		item2.setQty(1);
		item2.setPrice(4f);
		item2.setSubGList(null);
		item2.setOptList(null);

		OrderGoodsItem item3 = new OrderGoodsItem();
		item3.setgName("好家风环保纸杯");
		item3.setgId("GL000044");
		item3.setQty(1);
		item3.setPrice(8f);
		item3.setSubGList(null);
		item3.setOptList(null);

		List<OrderGoodsItem> list = new ArrayList<OrderGoodsItem>();
		list.add(item1);
		list.add(item2);
		list.add(item3);

		detail.setGoodsList(list);
		detail.setTotalPrice("27.88");
		detail.setContact("倪神州");
		detail.setSex(2);
		detail.setuPhone("18616742809");
		detail.setDeliverAddr("上海市杨浦区政立路300号101室");
		detail.setSelectedTime("尽快送达");
		detail.setsPhone("15000308988");

		Comment comment = new Comment();
		comment.setComment("我们都是小青蛙");
		comment.setcTime(1442211405);
		comment.setScore(4);
		comment.setReCommnet("我们都是癞蛤蟆");
		comment.setReTime(1442211775);

		detail.setComment(comment);
		detail.setAcceptTime(1442211405);
		result.data = detail;
		return result;
	}

}
